package com.db.drawio.template.db.table.api;

import com.db.drawio.template.db.table.conf.TableConfig;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;

public class TableTemplateApi {

    static String generatePath = "src/test/resources/templates/generate";

    private static VelocityEngine getVelocityEngine() {
        // 初始化 Velocity 引擎
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty("file.resource.loader.path", "src/main/resources/templates");
        velocityEngine.init();
        return velocityEngine;
    }


    public static void main(String[] args) {
        VelocityEngine velocityEngine = getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("drawio-tableframe-list.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        TableConfig tableConfig = new TableConfig();


        context.put("tableConfig", tableConfig);

        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());

        try (
                FileOutputStream fos = new FileOutputStream(new File(generatePath + "/test.html"));
        ) {
            fos.write(writer.toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
